package net.event.data_processing;

import net.event.data_processing.utils.FileUtils;
import net.event.data_processing.utils.RDF4JService;
import net.event.data_processing.utils.TDBService;
import org.apache.jena.riot.Lang;
import org.apache.jena.riot.RDFDataMgr;
import org.eclipse.rdf4j.rio.RDFFormat;
import org.eclipse.rdf4j.rio.Rio;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.List;
import java.util.Map;

public class Main {
    public static void main(String[] args) {
        rdf4jQueryYago();
    }

    public static void rdf4jQueryYago() {
        Map<String, String> sparqls = FileUtils.loadRules("data/source/framebase_rules/yagoIntegrationRulesSparql.txt");
        RDF4JService rdf4jYagoService = new RDF4JService("http://172.18.31.68:7200");
        for (String key : sparqls.keySet()) {
            System.out.println("begin query: " + key);
            String fileName = FileUtils.formatFileName("yago_" + key + ".ttl");
            File outFile = new File(fileName);
            while (outFile.exists()) {
                fileName += "_1.ttl";
                outFile = new File(fileName);
            }
            String sparql = sparqls.get(key);
            try {
                org.eclipse.rdf4j.model.Model model = rdf4jYagoService.execConstructSparql("yago", sparql);
                OutputStream out = new FileOutputStream(outFile);
                Rio.write(model, out, RDFFormat.NTRIPLES);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }


    public static void rdf4jQueryDBpedia() {
        RDF4JService rdf4jYagoService = new RDF4JService("http://172.18.31.66:7200");
        Map<String, String> sparqls1 = FileUtils.loadRules("data/source/framebase_rules/dbpedia-pf-rules.txt");
        for (String key : sparqls1.keySet()) {
            System.out.println("begin query: " + key);
            String fileName = FileUtils.formatFileName("dbpedia-pf_" + key + ".ttl");
            File outFile = new File(fileName);
            while (outFile.exists()) {
                fileName += "_1.ttl";
                outFile = new File(fileName);
            }
            String sparql = sparqls1.get(key);
            try {
                org.eclipse.rdf4j.model.Model model = rdf4jYagoService.execConstructSparql("dbpedia", sparql);
                OutputStream out = new FileOutputStream(outFile);
                Rio.write(model, out, RDFFormat.NTRIPLES);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        Map<String, String> sparqls2 = FileUtils.loadRules("data/source/framebase_rules/dbpediaEventsIntegrationRules.txt");
        for (String key : sparqls2.keySet()) {
            System.out.println("begin query: " + key);
            String fileName = FileUtils.formatFileName("dbpedia-events_" + key + ".ttl");
            File outFile = new File(fileName);
            while (outFile.exists()) {
                fileName += "_1.ttl";
                outFile = new File(fileName);
            }
            String sparql = sparqls2.get(key);
            try {
                org.eclipse.rdf4j.model.Model model = rdf4jYagoService.execConstructSparql("dbpedia", sparql);
                OutputStream out = new FileOutputStream(outFile);
                Rio.write(model, out, RDFFormat.NTRIPLES);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public static void rdf4jQueryWikiData() {
        Map<String, String> sparqls = FileUtils.loadRules("data/source/framebase_rules/wikidata-pf-rules.txt");
        RDF4JService rdf4jYagoService = new RDF4JService("http://172.18.31.67:7200");
        for (String key : sparqls.keySet()) {
            System.out.println("begin query: " + key);
            String fileName = FileUtils.formatFileName("wikidata_" + key + ".ttl");
            File outFile = new File(fileName);
            while (outFile.exists()) {
                fileName += "_1.ttl";
                outFile = new File(fileName);
            }
            String sparql = sparqls.get(key);
            try {
                org.eclipse.rdf4j.model.Model model = rdf4jYagoService.execConstructSparql("wikidata", sparql);
                OutputStream out = new FileOutputStream(outFile);
                Rio.write(model, out, RDFFormat.NTRIPLES);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public static void rdf4jQueryFreeBase() {
        Map<String, String> sparqls = FileUtils.loadRules("data/source/framebase_rules/freebaseSparqlIntegrationCFRules.txt");
        RDF4JService rdf4jYagoService = new RDF4JService("http://172.18.31.65:7200");
        for (String key : sparqls.keySet()) {
            System.out.println("begin query: " + key);
            String fileName = FileUtils.formatFileName("freebase_" + key + ".ttl");
            File outFile = new File(fileName);
            while (outFile.exists()) {
                fileName += "_1.ttl";
                outFile = new File(fileName);
            }
            String sparql = sparqls.get(key);
            try {
                org.eclipse.rdf4j.model.Model model = rdf4jYagoService.execConstructSparql("freebase", sparql);
                OutputStream out = new FileOutputStream(outFile);
                Rio.write(model, out, RDFFormat.NTRIPLES);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }


    public static void tdbQueryYago() {
        Map<String, String> sparqls = FileUtils.loadRules("data/source/framebase_rules/yagoIntegrationRulesSparql.txt");
        org.apache.jena.rdf.model.Model freebaseModel = TDBService.INSTANCE.getDefaultModel();
        for (String key : sparqls.keySet()) {
            System.out.println("begin query: " + key);
            String sparql = sparqls.get(key);
            String fileName = FileUtils.formatFileName("yago_" + key + ".ttl");
            File outFile = new File(fileName);
            while (outFile.exists()) {
                fileName += "_1.ttl";
                outFile = new File(fileName);
            }
            try {
                OutputStream out = new FileOutputStream(outFile);
                org.apache.jena.rdf.model.Model model = TDBService.INSTANCE.execConstructSparql(sparql, freebaseModel, TDBService.getPrologue("freebase"));
                RDFDataMgr.write(out, model, Lang.NTRIPLES);
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
    }

    //需要先用initdata.sh导入freebase数据（导入完了最好备份一个data/tdb_files），然后确保data/tdb_files存在并且是freebase的就可以执行这个
    public static void tdbQueryFreeBase() {
        Map<String, String> sparqls = FileUtils.loadRules("data/source/framebase_rules/freebaseSparqlIntegrationCFRules.txt");
        org.apache.jena.rdf.model.Model freebaseModel = TDBService.INSTANCE.getDefaultModel();
        for (String key : sparqls.keySet()) {
            System.out.println("begin query: " + key);
            String sparql = sparqls.get(key);
            String fileName = FileUtils.formatFileName("freebase_" + key + ".ttl");
            File outFile = new File(fileName);
            while (outFile.exists()) {
                fileName += "_1.ttl";
                outFile = new File(fileName);
            }
            try {
                OutputStream out = new FileOutputStream(outFile);
                org.apache.jena.rdf.model.Model model = TDBService.INSTANCE.execConstructSparql(sparql, freebaseModel, TDBService.getPrologue("freebase"));
                RDFDataMgr.write(out, model, Lang.NTRIPLES);
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
    }
    
}
